package cn.bigfire.compiler.dragon.appendixA.handwrite.symbols;

import cn.bigfire.compiler.dragon.appendixA.handwrite.lexer.Tag;
import cn.bigfire.compiler.dragon.appendixA.handwrite.lexer.Word;

public class Type extends Word {
    public int width = 0;

    //基础类型
    public static final Type
            Int = new Type("int", Tag.BASIC, 4),
            Float = new Type("float", Tag.BASIC, 8),
            Char = new Type("char", Tag.BASIC, 1),
            Bool = new Type("bool", Tag.BASIC, 1);

    public Type(String lexeme, int tag, int width) {
        super(lexeme, tag);
        this.width = width;
    }


    public static boolean numeric(Type type) {
        if (type == Type.Char || type == Type.Int || type == Type.Float) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断两个类型的 那个类型更大，自动返回更大的类型
     *
     * @param type1 类型1
     * @param type2 类型2
     * @return Type 大类型
     */
    public static Type max(Type type1, Type type2) {
        if (!numeric(type1) || !numeric(type2)) {
            return null;
        }

        if (type1 == Type.Float || type2 == Type.Float) {
            return Type.Float;
        }
        if (type1 == Type.Int || type2 == Type.Int) {
            return Type.Int;
        }
        return Type.Char;

    }

}
